const { Transform } = require('stream');
const _ = require('lodash');
const moment = require('moment');
const utils = require('../../utils');

module.exports = function (func) {
  const stream = new Transform({ objectMode: true });
  func = utils.parseFunction(func);
  stream._transform = async function (chunk, encoding, callback) {
    utils.debug('filterTransform [chunk]:', chunk);
    const r = func ? await func(chunk, { _, moment, utils }) : true;
    if (r) {
      stream.push(chunk);
    }
    callback();
  };

  stream.on('finish', function () {
    utils.debug('filterTransform [finish]');
  });

  return stream;
};